//
//  HomeScreenViewController.swift
//  SkylowAPP
//
//  Created by 青色石头 on 2024/9/23.
//

import UIKit
import SnapKit
import InputBarAccessoryView

class HomeScreenViewController: UIViewController {
    private lazy var titleHeader = UIImageView()
    private lazy var titleLbl = UILabel()
    
    private lazy var leftTitleView = {
        let stack = UIStackView()
        stack.spacing = 8
        stack.axis = .horizontal
        stack.alignment = .center
        
        titleHeader.layer.cornerRadius = 12
        titleHeader.clipsToBounds = true
        titleHeader.backgroundColor = .colorWith(hexString: "#333333")
        titleHeader.contentMode = .scaleAspectFill
        stack.addArrangedSubview(titleHeader)
        titleHeader.snp.makeConstraints { make in
            make.size.equalTo(24)
        }
        
        titleLbl.font = .systemFont(ofSize: 10, weight: .light)
        titleLbl.textColor = .white
        titleLbl.text = "Henry@Skylow"
        stack.addArrangedSubview(titleLbl)
        return stack
    }()
    
    private lazy var inputBar = {
        let inputBar = InputBarAccessoryView()
        inputBar.backgroundView.backgroundColor = .colorWith(hexString: "#333333")
        inputBar.inputTextView.placeholder = "Aa"
        inputBar.inputTextView.textColor = .white
        inputBar.separatorLine.isHidden = true
        inputBar.sendButton.title = "发送"
        
        view.addSubview(inputBar)
        inputBar.snp.makeConstraints { make in
            make.leading.trailing.equalToSuperview()
            make.bottom.equalTo(self.view.safeAreaLayoutGuide.snp.bottom)
        }
        return inputBar
    }()
    
    private lazy var headerImg = {
        let img = UIImageView(image: UIImage(named: "home_screen_bg_icon"))
        return img
    }()
    
    private lazy var tableView = {
        let tb = UITableView(frame: .zero, style: .grouped)
        tb.delegate = self
        tb.dataSource = self
        tb.tableHeaderView = self.headerImg
        tb.backgroundColor = .colorWith(hexString: "#333333")
        tb.sectionFooterHeight = 0
        tb.sectionHeaderHeight = 0
        tb.separatorStyle = .none
        if #available(iOS 15.0, *) {
            tb.sectionHeaderTopPadding = 0
        }
        tb.register(UINib(nibName: "HomeScreenContentCell", bundle: nil), forCellReuseIdentifier: "text_content")
        tb.register(UINib(nibName: "HomeScreenCommentCell", bundle: nil), forCellReuseIdentifier: "comment_cell")
        
        view.addSubview(tb)
        tb.snp.makeConstraints { make in
            make.leading.trailing.equalToSuperview()
            make.top.equalTo(self.view.safeAreaLayoutGuide.snp.top)
            make.bottom.equalTo(self.inputBar.snp.top)
        }
        return tb
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        var lefts = navigationItem.leftBarButtonItems ?? []
        lefts.append(UIBarButtonItem(customView: self.leftTitleView))
        navigationItem.leftBarButtonItems = lefts
        _ = inputBar
        _ = tableView
        
        
    }
    
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        headerImg.frame = CGRectMake(0, 0, view.width, view.width * (523.0 / 430.0))
    }

}

extension HomeScreenViewController: UITableViewDataSource {
    func numberOfSections(in tableView: UITableView) -> Int {
        2
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if section == 0 {
            return 1
        }
        return 3
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if indexPath.section == 0 && indexPath.row == 0 {
            let cell = tableView.dequeueReusableCell(withIdentifier: "text_content", for: indexPath)
            return cell
        }
        let cell = tableView.dequeueReusableCell(withIdentifier: "comment_cell", for: indexPath)
        return cell
    }
    
    
}

extension HomeScreenViewController: UITableViewDelegate {
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
    }
    
}
